package com.laijiale.mapper;

import com.laijiale.entities.Dept;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:51
 */
@Repository
public interface DeptMapper {
    @Insert("insert into ums_dept (title,info,fk_room_id,deleted,created,updated )" +
            "values(#{title},#{info},#{fkRoomId},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Dept dept);


    @Select("select ud.id, ud.title,ud.info,ud.fk_room_id, ud.deleted, ud.created, ud.updated \n" +
            "from ums_dept ud" +
            " where ud.id=#{param1} and ud.deleted=0")
    Dept selectById(Long id);

    @Update("<script>" +
            "update ums_dept" +
            "<set>" +
            "<if test='title !=null'>title=#{title},</if> " +
            "<if test='info !=null'>info=#{info},</if> " +
            "<if test='fkRoomId !=null'>fk_room_id=#{fkRoomId},</if> " +

            "<if test='deleted !=null'>deleted=#{deleted},</if> " +

            "<if test='created !=null'>created=#{created},</if> " +

            "<if test='updated !=null'>updated=#{updated},</if> " +
            "</set> " +
            "where id =#{id}  and deleted =0" +
            "</script>")
    int updatedById(Dept dept);

    @Update("update ums_dept set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);
    @Update("<script>" +
            "update ums_dept set deleted =1,updated = now() where " +
            "<foreach collection = 'list' item='e' open ='id in (' close= ')' separator = ','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);
    @Results(id = "resultMap",value = {
            @Result(column = "id",property = "id",id = true),
            @Result(column = "fk_room_id" ,property = "fkRoomId"),
            @Result(column = "fk_room_id",property = "room",
                    one = @One(select = "com.laijiale.mapper.RoomMapper.selectById")),
    })
    @Select("select  ud.id, ud.title, ud.info, ud.fk_room_id,ud.deleted, " +
            "ud.created,ud.updated  from ums_dept ud ")
    List<Dept> list();

    @Select("select ud.id, ud.title from ums_dept ud " +
            "where ud.title = #{title} ")
     Dept selectByAllTitle(Dept dept);

    @Delete("delete from ums_dept where id =#{param1}")
    int deleteById(Long id);

    @Select("select ud.id, ud.title from ums_dept ud " +
            "where ud.title = #{param1} ")
    Dept selectByTitle(String title);

    @Select("select count(*)>0 from ums_dept ud where ud.fk_room_id = #{param1} and ud.deleted = 0 " )
    boolean existsRoomId(Long fkRoomId);

    @Select("<script>" +
            "select count(*)>0 from ums_dept ud where " +
            "<foreach collection = 'list' item='e' open ='ud.fk_room_id in (' close=')' separator = ','>" +
            "${e}" +
            "</foreach> " +
            "and ud.deleted = 0" +
            "</script>")
    boolean existsRoomIds(List<Long> fkRoomIds);
    @ResultMap("resultMap")
    @Select("select  ud.id, ud.title, ud.info, ud.fk_room_id,ud.deleted, " +
            "ud.created,ud.updated  from ums_dept ud "+
            "where ud.deleted=0 and ud.title like concat('%',#{param1},'%') "
    )
    List<Dept> listLikeTitle(String title);



}
